package com.lwp.ctable.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.exception.NotLoginException;
import com.lwp.ctable.model.CResult;
import com.lwp.ctable.model.Image;
import com.lwp.ctable.service.serviceImp.ImageService;
import com.lwp.ctable.utils.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ImageController
 * @Description TODO
 * @Author kaka
 * @Date 2021/4/15 11:13
 * @Version 1.0
 * @Location HANGZHOU
 **/
@SaCheckLogin
@RestController
@RequestMapping("/image")
public class ImageController {

    @Autowired
    OssUtil ossUtil;
    @Autowired
    ImageService imageService;

    @RequestMapping("/uploadImages")
    public Object uploadImages(@RequestParam("file") MultipartFile fileList,@RequestParam("user_id") String userId)
    {
        try {//调用OSS工具类
            String url = ossUtil.uploadFile(fileList);
            Map<String, Object> returnBody = new HashMap<>();
            Map<String, Object> returnMap = new HashMap<>();
            //存储图片数据
            Image image = new Image();
            image.setUserId(Long.valueOf(userId));
            image.setUrl(url);
            int ret=imageService.insert(image);
            if (ret<=0){
                returnBody.put("data",null);
                returnBody.put("code","400");
                returnBody.put("message","上传失败");
            }
            else {
                returnMap.put("url", url);
                returnBody.put("data",returnMap);
                returnBody.put("code","200");
                returnBody.put("message","上传成功");
            }
            return returnBody;
        }
        catch (Exception e) {
            Map<String, Object> returnBody = new HashMap<>();
            returnBody.put("data",null);
            returnBody.put("code","400");
            returnBody.put("message","上传失败");
            return returnBody;
        }
    }
}